import { queryUsers, queryDepartment, queryStaff, queryCurrent, changeCurrent, changePassword } from '@/services/user';
import { message } from 'antd';

export default {
  namespace: 'user',

  state: {
    visible: false,
    list: [],
    currentUser: {},
    department: [],
    position: []
  },

  effects: {
    * fetchCurrent(_, { call, put }) {
      const { data = {} } = yield call(queryCurrent, {
        type: 1
      });
      // eslint-disable-next-line
      const { staff_name = '', staff_header = '', staff_department = '', staff_tel = 0, } = data
      yield put({
        type: 'saveCurrentUser',
        payload: {
          name: staff_name,
          avatar: staff_header,
          department: staff_department,
          phone: staff_tel,
        },
      });
    },
    * fetchDepartment(_, { call, put }) {
      const { data = [] } = yield call(queryDepartment);
      yield put({
        type: 'save',
        payload: { department: data },
      });
    },
    * fetchStaff(_, { call, put }) {
      const { payload } = _
      const { data } = yield call(queryStaff, {
        ...payload
      });
      console.log(data, 'department123')
      yield put({
        type: 'save',
        payload: { staff: data },
      });
    },
    * fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: { list: response },
      });
    },
    // 修改个人头像
    * changeCurrent({ payload = {} }, { call, put, select }) {
      const header = payload.avatar
      const params = {
        type: 2,
        header,
      }
      if (!header) {
        message.success('请上传头像修改')
        return;
      }
      const { status } = yield call(changeCurrent, params);
      const { currentUser } = yield select(_ => _.user)
      if (status) {
        yield put({
          type: 'saveCurrentUser',
          payload: {
            ...currentUser,
            ...params
          },
        });
        message.success('信息修改成功')
      } else {
        message.error('信息修改失败')
      }
    },
    * changePassword({ payload = {} }, { put, call }) {
      const { status } = yield call(changePassword, {
        old_pwd: payload.current,
        pwd: payload.password,
        rpwd: payload.confirm
      });
      if (status) {
        yield put({
          type: 'save',
          payload: {
            visible: false
          },
        });
        message.success('密码修改成功')
      } else {
        message.error('密码修改失败')
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: {
          ...action.payload,
          userid: '001',
          email: 'xxx@qq.com',
          signature: '爱你一万年',
          title: '全栈工程师',
          group: '夏至信息科技有限公司',
          tags: [{ key: '0', label: 'leader', }, { key: '1', label: '专注 Hello World', }],
          notifyCount: 12,
          country: 'China',
          geographic: { province: { label: '浙江省', key: '330000', }, city: { label: '杭州市', key: '330100', }, },
          address: '西湖区工专路 77 号'
        }
      };
    },
    saveDepartment(state, action) {
      return {
        ...state,
        department: {
          ...state.department,
          ...action.payload,
        },
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
  },
};